﻿/*
 * Mr.Chau
 * 27.05.2013
 * Nhập sản phẩm từ file excell với mẫu định sẵn
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NangLuongTieuDiemBUS.ACK;
using NangLuongTieuDiemBUS;
using NangLuongTieuDiemBUS.SDK;
using NangLuongTieuDiemDTO.DTO;


namespace NangLuongTieuDiemGUI.NangLuongTieuDiemUserControl
{
    public partial class ucNhapSanPhamTuFileExcel : UserControl
    {
        #region "[Tạo event và delegate để mở usercontrol Lập Hóa đơn]"
        public delegate void ClearItem();
        //public event ClearItem ItemEvent;
        public DelegateShowItem showItem = null;

       // public event ClearItem showUcNhapSanPhamEvent;
        public DelegateShowItem showUcNhapSanPhamItem = null;
        #endregion

        public ucNhapSanPhamTuFileExcel()
        {
            InitializeComponent();
        }

        #region "[Quay lại bước nhập sản phẩm thủ công]"
        private void btnNhapThuCong_Click(object sender, EventArgs e)
        {
            try {
                //if (showUcNhapSanPhamEvent != null)
                //    showUcNhapSanPhamEvent();

                if (showUcNhapSanPhamItem != null)
                    showUcNhapSanPhamItem(false);
            }
            catch(Exception ex)
            {
                InSert_LoiHeThong("btnNhapThuCong_Click", ex);
            }
        }
        #endregion

        #region "[Nhập sản phẩm]"
        private void btnNhapSanPham_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnNhapSanPham_Click", ex);
            }
        }
        #endregion

        #region "[Xóa dữ liệu]"
        private void btnXoaDuLieu_Click(object sender, EventArgs e)
        {
            try { ClearControl(); }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnXoaDuLieu_Click", ex);
            }
        }
        #endregion

        #region "[Thoát khỏi chức năng này]"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            try
            {
                //if (ItemEvent != null)
                //    ItemEvent();

                if (showItem != null)
                    showItem(false);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnThoat_Click", ex);
            }
        }
        #endregion

        #region "[Hiển thị sản phẩm xuống datagridview]"
        private void btnHienThi_Click(object sender, EventArgs e)
        {
            try { 
                if(txtDuongDan.Text.Trim() == string.Empty)
                {
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Chưa chọn đường dẫn đến file excel","Thông Báo",true);
                    return;
                }
                if(numericUpDown1.Value<1)
                {
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Sheet của Excel không được nhỏ hơn 1","Thông Báo",true);
                    return;
                }
                //lấy thông tin tất cả các sản phẩm.
                SanPhamACK ItemResult = NangLuongTieuDiem_Management.SanPham_GetListFromFileExcel(txtDuongDan.Text.Trim(), (int)numericUpDown1.Value);

                #region "[kiểm tra cờ từng list sản phẩm]"
                bool isFlagPin = ItemResult.Pins.Count() > 0 ? true : false;
                bool isFlagAcquy = ItemResult.Acquys.Count() > 0 ? true : false;
                bool isFlagThietBiDien = ItemResult.ThietBiDiens.Count() > 0 ? true : false;
                bool isFlagBoDieuKhienSac = ItemResult.BoDieuKhienSacs.Count() > 0 ? true : false;
                #endregion

                if (isFlagPin)
                {
                    if (isFlagAcquy)
                    {
                        if (isFlagThietBiDien)
                        {
                            if (isFlagBoDieuKhienSac)
                            {
                                //add 4 list lên 4 control pannel
                                #region "[add 4 list lên 4 control pannel]"
                                splitContainerControl1.Panel1.Controls.Clear();
                                splitContainerControl1.Panel2.Controls.Clear();

                                //add control Pin
                                ucLuoiHienThiPinNangLuong ucPin = new ucLuoiHienThiPinNangLuong(ItemResult.Pins);
                                ucPin.Dock = DockStyle.Fill;
                                splitContainerControl1.Panel1.Controls.Add(ucPin);

                                SplitContainer split1 = new SplitContainer();
                                split1.Dock = DockStyle.Fill;
                                split1.Orientation = Orientation.Horizontal;
                                ucLuoiHienThiPinNangLuong ucAcquy = new ucLuoiHienThiPinNangLuong(ItemResult.Acquys);
                                ucAcquy.Dock = DockStyle.Fill;
                                split1.Panel1.Controls.Add(ucAcquy);


                                SplitContainer split2 = new SplitContainer();
                                split2.Dock = DockStyle.Fill;
                                split2.Orientation = Orientation.Horizontal;
                                ucLuoiHienThiPinNangLuong ucThietBiDien = new ucLuoiHienThiPinNangLuong (ItemResult.ThietBiDiens);
                                ucThietBiDien.Dock = DockStyle.Fill;
                                split2.Panel1.Controls.Add(ucThietBiDien);

                                ucLuoiHienThiPinNangLuong ucDieuKhienSac = new ucLuoiHienThiPinNangLuong(ItemResult.BoDieuKhienSacs);
                                ucDieuKhienSac.Dock = DockStyle.Fill;
                                split2.Panel2.Controls.Add(ucDieuKhienSac);

                                split1.Panel2.Controls.Add(split2);

                                splitContainerControl1.Panel2.Controls.Add(split1);
                                #endregion

                            }
                            else
                            {
                                //add 3 list:Pin, acquy, thietbidien
                                #region "[add 3 list:Pin, acquy, thietbidien]"
                                splitContainerControl1.Panel1.Controls.Clear();
                                splitContainerControl1.Panel2.Controls.Clear();

                                //add control Pin
                                ucLuoiHienThiPinNangLuong ucPin = new ucLuoiHienThiPinNangLuong(ItemResult.Pins);
                                ucPin.Dock = DockStyle.Fill;
                                splitContainerControl1.Panel1.Controls.Add(ucPin);

                                SplitContainer split1 = new SplitContainer();
                                split1.Dock = DockStyle.Fill;
                                split1.Orientation = Orientation.Horizontal;
                                ucLuoiHienThiPinNangLuong ucAcquy = new ucLuoiHienThiPinNangLuong(ItemResult.Acquys);
                                ucAcquy.Dock = DockStyle.Fill;
                                split1.Panel1.Controls.Add(ucAcquy);

                                ucLuoiHienThiPinNangLuong ucThietBiDien = new ucLuoiHienThiPinNangLuong(ItemResult.ThietBiDiens);
                                ucThietBiDien.Dock = DockStyle.Fill;
                                split1.Panel2.Controls.Add(ucThietBiDien);

                                splitContainerControl1.Panel2.Controls.Add(split1);
                                #endregion
                            }
                        }
                        else
                        {
                            //add 2 list: Pin, acquy
                            #region "[add 2 list: Pin, acquy]"
                            splitContainerControl1.Panel1.Controls.Clear();
                            splitContainerControl1.Panel2.Controls.Clear();

                            //add control Pin
                            ucLuoiHienThiPinNangLuong ucPin = new ucLuoiHienThiPinNangLuong(ItemResult.Pins);
                            ucPin.Dock = DockStyle.Fill;
                            splitContainerControl1.Panel1.Controls.Add(ucPin);

                            ucLuoiHienThiPinNangLuong ucAcquy = new ucLuoiHienThiPinNangLuong(ItemResult.Acquys);
                            ucAcquy.Dock = DockStyle.Fill;
                            splitContainerControl1.Panel2.Controls.Add(ucAcquy);
                            #endregion
                        }
                    }
                    else
                    {
                        //add 1 list Pin
                        #region "[add 1 list Pin]"
                        panelControl1.Controls.Clear();
                        //add control Pin
                        ucLuoiHienThiPinNangLuong ucPin = new ucLuoiHienThiPinNangLuong(ItemResult.Pins);
                        ucPin.Dock = DockStyle.Fill;
                        panelControl1.Controls.Add(ucPin);
                        #endregion
                    }
                }
                else if (isFlagAcquy)
                {
                    if (isFlagThietBiDien)
                    {
                        if (isFlagBoDieuKhienSac)
                        {
                            //add 3 list : acquy, thiet bi dien, dieukhiensac
                            #region "[add 3 list : acquy, thiet bi dien, dieukhiensac]"
                            splitContainerControl1.Panel1.Controls.Clear();
                            splitContainerControl1.Panel2.Controls.Clear();

                            //add control Acquy
                            ucLuoiHienThiPinNangLuong ucAcquy = new ucLuoiHienThiPinNangLuong(ItemResult.Acquys);
                            ucAcquy.Dock = DockStyle.Fill;
                            splitContainerControl1.Panel1.Controls.Add(ucAcquy);

                            SplitContainer split1 = new SplitContainer();
                            split1.Dock = DockStyle.Fill;
                            split1.Orientation = Orientation.Horizontal;

                            ucLuoiHienThiPinNangLuong ucThietBiDien = new ucLuoiHienThiPinNangLuong(ItemResult.ThietBiDiens);
                            ucThietBiDien.Dock = DockStyle.Fill;
                            split1.Panel1.Controls.Add(ucThietBiDien);

                            ucLuoiHienThiPinNangLuong ucDieuKhienSac = new ucLuoiHienThiPinNangLuong(ItemResult.BoDieuKhienSacs);
                            ucDieuKhienSac.Dock = DockStyle.Fill;
                            split1.Panel2.Controls.Add(ucDieuKhienSac);

                            splitContainerControl1.Panel2.Controls.Add(split1);
                            #endregion
                        }
                        else
                        {
                            //add 2 list:acquy, thietbidien
                            #region "[add 2 list:acquy, thietbidien]"
                            splitContainerControl1.Panel1.Controls.Clear();
                            splitContainerControl1.Panel2.Controls.Clear();

                            //add control acquy

                            ucLuoiHienThiPinNangLuong ucAcquy = new ucLuoiHienThiPinNangLuong(ItemResult.Acquys);
                            ucAcquy.Dock = DockStyle.Fill;
                            splitContainerControl1.Panel1.Controls.Add(ucAcquy);

                            ucLuoiHienThiPinNangLuong ucThietBiDien = new ucLuoiHienThiPinNangLuong(ItemResult.ThietBiDiens);
                            ucThietBiDien.Dock = DockStyle.Fill;
                            splitContainerControl1.Panel2.Controls.Add(ucThietBiDien);
                            #endregion
                        }
                    }
                    else
                    {
                        //add 1 list:  acquy
                        #region "[add 1 list:  acquy]"

                        panelControl1.Controls.Clear();
                        //add control acquy
                        ucLuoiHienThiPinNangLuong ucAcquy = new ucLuoiHienThiPinNangLuong(ItemResult.Acquys);
                        ucAcquy.Dock = DockStyle.Fill;
                        panelControl1.Controls.Add(ucAcquy);
                        #endregion
                    }
                }
                else if (isFlagThietBiDien)
                {
                    if (isFlagBoDieuKhienSac)
                    {
                        //add 2 list :thiet bi dien, dieukhiensac
                        #region "[add 2 list :thiet bi dien, dieukhiensac]"
                        splitContainerControl1.Panel1.Controls.Clear();
                        splitContainerControl1.Panel2.Controls.Clear();

                        //add control acquy

                        ucLuoiHienThiPinNangLuong ucThietBiDien = new ucLuoiHienThiPinNangLuong(ItemResult.ThietBiDiens);
                        ucThietBiDien.Dock = DockStyle.Fill;
                        splitContainerControl1.Panel1.Controls.Add(ucThietBiDien);

                        ucLuoiHienThiPinNangLuong ucDieuKhienSac = new ucLuoiHienThiPinNangLuong(ItemResult.BoDieuKhienSacs);
                        ucDieuKhienSac.Dock = DockStyle.Fill;
                        splitContainerControl1.Panel2.Controls.Add(ucDieuKhienSac);
                        #endregion
                    }
                    else
                    {
                        //add 1 list:thietbidien
                        #region "[add 1 list:thietbidien]"
                        panelControl1.Controls.Clear();
                        //add control acquy
                        ucLuoiHienThiPinNangLuong ucThietBiDien = new ucLuoiHienThiPinNangLuong(ItemResult.ThietBiDiens);
                        ucThietBiDien.Dock = DockStyle.Fill;
                        panelControl1.Controls.Add(ucThietBiDien);
                        #endregion
                    }
                }
                else if (isFlagBoDieuKhienSac)
                { 
                    #region "[add 1 list bo dieu khien sạc"]
                    panelControl1.Controls.Clear();
                    //add control acquy
                    ucLuoiHienThiPinNangLuong ucDieuKhienSac = new ucLuoiHienThiPinNangLuong(ItemResult.BoDieuKhienSacs);
                    ucDieuKhienSac.Dock = DockStyle.Fill;
                    panelControl1.Controls.Add(ucDieuKhienSac);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnHienThi_Click", ex);
            }
        }
        #endregion

        #region "[lấy đường dẫn]"
        private void txtDuongDan_DoubleClick(object sender, EventArgs e)
        {
            try {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "File Excell|*.xls";
                DialogResult result = open.ShowDialog();
                if (result == DialogResult.OK)
                { 
                    //lấy đường dẫn.
                    txtDuongDan.Text = open.FileName;  
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("txtDuongDan_DoubleClick", ex);
            }
        }
        #endregion

        #region "[Xóa dữ liệu]"
        private void ClearControl()
        {
            try {
                txtDuongDan.Text = string.Empty;
                //dgvHienThi.DataSource = null;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("ClearControl", ex);
            }
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            NangLuongTieuDiem_Management.LoiHeThong_Insert(lhtDto);
            MessageBox.Show(lhtDto.TenLoi);
        }
        #endregion
    }
}
